package com.service.gzp.impl;

import com.dao.RoletreeDao;
import com.dao.TreeDao;
import com.dao.UserinfoDao;
import com.dao.UserinforoleDao;
import com.entity.*;
import com.service.gzp.TreeService;
import com.util.gzp.AjaxResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName: TreeServiceImpl
 * Package: com.service.gzp.impl
 *
 * @author: guanzepeng
 * @Date: 2021/4/5 23:57 星期一
 * @Description: 加载用户菜单业务层
 */
@Service
@Slf4j
public class TreeServiceImpl implements TreeService {

	@Autowired
	private TreeDao treeDao;

	@Autowired
	private UserinfoDao userinfoDao;

	@Autowired
	private UserinforoleDao userinforoleDao;

	@Autowired
	private RoletreeDao roletreeDao;

	@Transactional
	@Override
	public AjaxResponse getMenusByUserName(String username) {
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		//不查询已删除的用户
		criteria.andIsdeleteEqualTo(false);
		//传入用户名
		criteria.andUnameEqualTo(username);
		//查询出来当前登录用户的信息
		Userinfo userinfo = userinfoDao.selectByExample(userinfoExample).get(0);
		//查询出角色ID
		UserinforoleExample userinforoleExample = new UserinforoleExample();
		userinforoleExample.createCriteria().andUidEqualTo(userinfo.getUid());
		Integer rid = userinforoleDao.selectByExample(userinforoleExample).get(0).getRid();
		//查询出角色所对应的权限ID
		RoletreeExample roletreeExample = new RoletreeExample();
		roletreeExample.createCriteria().andRidEqualTo(rid);
		List<RoletreeKey> roletreeKeys = roletreeDao.selectByExample(roletreeExample);
		//查询出所有的权限
		TreeExample treeExample = new TreeExample();
		TreeExample.Criteria criteria1 = treeExample.createCriteria();
		List<Integer> list = new ArrayList<>();
		for (RoletreeKey root : roletreeKeys) {
			list.add(root.getTid());
		}
		criteria1.andTidIn(list);
		List<Tree> trees = treeDao.selectByExample(treeExample);

		return AjaxResponse.success(trees);
	}

	/**
	 * 根据传入的角色ID查询权限ID
	 * @param rid 传入角色ID
	 * @return 返回数组,对应菜单ID
	 */
	@Override
	public AjaxResponse getQuanXian(Integer rid) {
		if(null == rid) {
			return AjaxResponse.success("输入有误,查询失败!",500);
		}
		RoletreeExample roletreeExample = new RoletreeExample();
		RoletreeExample.Criteria criteria = roletreeExample.createCriteria();
		criteria.andRidEqualTo(rid);
		List<RoletreeKey> roletreeKeys = roletreeDao.selectByExample(roletreeExample);
		if(0 == roletreeKeys.size()) {
			return AjaxResponse.success(new int[0]);
		}
		int[] arr = new int[roletreeKeys.size()];
		for (RoletreeKey r: roletreeKeys) {
			for (int i = 0; i < arr.length; i++) {
				if(arr[i] == 0) {
					arr[i] = r.getTid();
					break;
				}
			}
		}
		log.info("执行getQuanXian方法得到的权限数组为:"+ Arrays.toString(arr));
		return AjaxResponse.success(arr);
	}

	/**
	 * 批量新增角色权限
	 * @param rid 角色ID
	 * @param tid 权限ID数组
	 * @return 返回成功失败
	 */
	@Override
	@Transactional
	public AjaxResponse updateQuanXian(Integer rid, int[] tid) {
		RoletreeKey roletreeKey = new RoletreeKey();
		roletreeKey.setRid(rid);
		boolean b = roletreeDao.deleteByPrimaryKey(roletreeKey) != 0;
		if(!b) {
			return AjaxResponse.success("修改权限失败!",500);
		}
		if(0 == tid.length) {
			b = roletreeDao.updateQuanXian(rid, new int[]{9,36});
		}
		b = roletreeDao.updateQuanXian(rid, tid);
		if(!b) {
			return AjaxResponse.success("修改权限失败!",500);
		}
		return AjaxResponse.success();
	}

	/**
	 * 查询当前用户所拥有的菜单ID
	 * @param uid 传入用户ID
	 * @return 返回数组
	 */
	@Override
	public AjaxResponse findByTree(Integer uid) {
		UserinforoleExample userinforoleExample = new UserinforoleExample();
		userinforoleExample.createCriteria().andUidEqualTo(uid);
		List<UserinforoleKey> userinforoleKeys = userinforoleDao.selectByExample(userinforoleExample);
		if(0 == userinforoleKeys.size()) {
			return AjaxResponse.success("菜单初始化失败,请重新尝试!",500);
		}
		List<Integer> l = new ArrayList<>();
		for (UserinforoleKey u : userinforoleKeys) {
			l.add(u.getRid());
		}
		RoletreeExample roletreeExample = new RoletreeExample();
		RoletreeExample.Criteria criteria = roletreeExample.createCriteria();
		criteria.andRidIn(l);
		List<RoletreeKey> roletreeKeys = roletreeDao.selectByExample(roletreeExample);
		if(0 == roletreeKeys.size()) {
			return AjaxResponse.success("菜单初始化失败,请重新尝试!",500);
		}
		int[] arr = new int[roletreeKeys.size()];
		for (RoletreeKey r : roletreeKeys) {
			for (int i = 0; i < roletreeKeys.size(); i++) {
				if(0 == arr[i]) {
					arr[i] = r.getTid();
					break;
				}
			}
		}
		return AjaxResponse.success(arr);
	}
}
